package com.xiyuan.smartutils.template.engine.expression.primitive;

import com.xiyuan.smartutils.StrUtils;
import com.xiyuan.smartutils.template.engine.VariableMap;
import com.xiyuan.smartutils.template.engine.exception.ExpressionException;

/**
 * 整数原型，byte/short/int/long，非零数字开头数字连贯出现"^[1-9]\\d*$"<br>
 * <br>
 * 但是实际中可能出现0123，这种情况，和点号一起组合成_Decimal
 *
 * @version v1.0.0 @author lgz 2020年11月1日 新建与整理
 * @see Byte 一个字节
 * @see Short 二个字节
 * @see Integer 四个字节
 * @see Long 八个字节
 */
public class _Integer extends Primitive {
    private static final long serialVersionUID = 1L;
    private String value;
    private boolean isLong = false;
    
    public _Integer(String path, int begin, int column, String value, boolean isLong) {
        super(path, begin, column);
        this.value = value;
        this.isLong = isLong;
    }
    
    @Override
    public int getType() {
        return INTEGER;
    }
    
    @Override
    public Object build(VariableMap variableMap) throws ExpressionException {
        return isLong ? getLong() : getInt();
    }
    
    @Override
    public String toString() {
        return value;
    }
    
    public long getLong() {
        return Long.parseLong(StrUtils.trimRight(value.toLowerCase(), "l"));
    }
    
    public int getInt() {
        return (int) getLong();
    }
    
    public short getShort() {
        return (short) getLong();
    }
    
    public byte getByte() {
        return (byte) getLong();
    }
}
